<template>
  <div class="router-container">
    <div class="container">
      <div class="inner_container">
        <div class="table">
          <el-form size="mini">
            <el-form-item>
              <el-button v-has-permi="['manage:building:save']" @click="showDialog(undefined)" size="mini" type="primary">
                新增
              </el-button>
            </el-form-item>
          </el-form>

          <el-table :expand-row-keys="defaultExpand" :data="list" v-loading="loading" :tree-props="{children: 'children'}" row-key="id">
            <el-table-column label="名称" prop="name"></el-table-column>
            <el-table-column label="宿舍数量" prop="roomNum"></el-table-column>
            <el-table-column label="人数" prop="studentNum"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button v-has-permi="['manage:building:query']"
                           size="mini" type="primary" @click="showDialog(scope.row.id)">详情
                </el-button>
                <template>
                  <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
                    <el-button style="margin-left: 8px" v-has-permi="['manage:building:delete']"
                               slot="reference"
                               size="mini" type="danger">删除
                    </el-button>
                  </el-popconfirm>
                </template>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="dialog">
          <el-dialog
              :title="dialogTitle"
              :visible.sync="dialogVisible"
              width="45%"
              @closed="handleClose"
          >
            <el-form ref="saveOrUpdateForm" :rules="rules" :model="building" label-width="80px">
              <el-form-item label="上级">
                <tree-select
                    v-model="building.parentId"
                    :options="list"
                    :normalizer="normalizer"
                    :show-count="true"
                    placeholder="选择上级">
                </tree-select>
              </el-form-item>
              <el-form-item label="名称" prop="name">
                <el-input v-model="building.name" placeholder="输入名称"></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button type="danger" @click="handleClose">取 消</el-button>
              <el-button type="primary" v-has-permi="['manage:building:update']" @click="saveOrUpdate">保 存</el-button>
            </span>
          </el-dialog>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { list, query, del, saveOrUpdate } from "@/api/building";
import TreeSelect from '@riophae/vue-treeselect'
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "index",
  // 注册 TreeSelect 组件
  components:{ TreeSelect },
  data() {
    return {
      // 楼列表数据
      list:[],
      // 楼信息对象
      building:{
        id: undefined,
        name: undefined
      },
      // 加载状态
      loading: undefined,
      // 表单验证规则
      rules: {
        name: {required: true, message: '名称不能为空', trigger: 'blur'}
      },
      // 默认展开的节点
      defaultExpand: [],
      // 弹出框是否可见
      dialogVisible: false,
      // 弹出框标题
      dialogTitle: '添加'
    }
  },
  methods: {
    // 用于将楼节点转换成 TreeSelect 所需的数据格式
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    // 保存或更新楼信息
    saveOrUpdate() {
      const _this = this
      this.$refs['saveOrUpdateForm'].validate((valid) => {
        if (valid) {
          // 调用后端API保存或更新楼信息
          saveOrUpdate(this.building).then(res => {
            if (res.statusCode === 200) {
              // 显示成功消息
              this.$message({
                message: res.msg,
                type: 'success'
              });
              // 关闭弹出框并刷新列表数据
              _this.handleClose()
              _this.listData()
            }
          })
        } else {
          // 显示错误消息
          this.$message({
            message: '请正确填写参数',
            type: 'error'
          });
          return false;
        }
      })
    },
    // 显示添加或修改楼信息的弹出框
    async showDialog(id) {
      this.dialogTitle = '添加楼'
      if (id) {
        this.dialogTitle = '修改楼'
        // 根据ID查询楼信息
        const {data} = await query(id)
        if (data) {
          this.building = data
          this.dialogVisible = true
        }
      } else {
        this.dialogVisible = true
      }
    },
    // 关闭弹出框并重置表单数据
    handleClose() {
      this.building = {
        id: undefined,
        name: undefined
      }
      this.$refs['saveOrUpdateForm'].resetFields()
      this.dialogVisible = false
    },
    // 删除楼信息
    del(id) {
      // 调用后端API删除楼信息
      del(id).then(res => {
        if (res.statusCode === 200) {
          // 显示删除成功消息，并刷新列表数据
          this.$message({
            message: res.msg,
            type: 'success'
          });
          this.listData()
        }
      })
    },
    // 获取楼列表数据
    async listData() {
      this.loading = true
      // 调用后端API获取楼列表数据
      const {data} = await list()
      this.list = data
      this.loading = false
      // 将所有楼节点的ID添加到默认展开的节点中
      for (let i of this.list) {
        this.defaultExpand.push(i.id.toString())
      }
    }
  },
  created() {
    // 组件创建时执行查询
    this.listData()
  }
}
</script>


<style scoped>
.table {
  padding: 10px;
}

.router-container {
  height: 100%;
}
</style>
